<?PHP
class Controller_Test extends Controller_Base
{
    function printAction()
    {echo '21';
    
    }
    function showAction($url = null)
    {
     if(!$this->currentUser)
        {
            $this->redirect('/auth');
        }
        $login=$this->currentUser;
        $rgPages=$this->dbInstance->select('users', array(
            'name',
            'login'
        ),array("login" => $login
            ));
        print_r($rgPages);

        if(!$rgPages)
        {
            $rgPages=array();
        }
        $this->view->rgPages=$rgPages;
        $this->view->render('main',$rgPages);
    }
 
    function updateAction()
    {
        $id=$this->getParam('id');
        $rgFields=array(
            'name'         => $this->getParam('name')
        );
        if(!$this->currentUser)
        {
            $this->redirect('/test/auth');
        }
        if(!$id)
        {
            $this->redirect('/test/show');
        }
        $this->dbInstance->update('some_table', $rgFields, array('id'=>$id));
        $this->redirect('/test/show');
    }
 
    function addAction()
    {
        if(!$this->currentUser)
        {
            $this->redirect('/test/auth');
        }
        $rgFields=array(
            'name'         => $this->getParam('name')
        );
        $this->dbInstance->insert('some_table', $rgFields);
        $this->redirect('/test/show');
    }
 
    function deleteAction()
    {
        if(!$this->currentUser)
        {
            $this->redirect('/test/auth');
        }
        $id = $this->getParam('id');
        if(!$id)
        {
            $this->redirect('/test/show');
        }
        $this->dbInstance->delete('some_table', array('id'=>$id));
        $this->redirect('/test/show');
    }
 
    function authAction()
    {
        $this->view->render('auth');
    }
 
    function loginAction()
    {
        /*
        Here we log in user. If success, redirect to /test/show
        If fails, redirect to /test/auth
        */
     $login = mysql_real_escape_string(( get_magic_quotes_gpc() )? stripslashes($_POST['login']): $_POST['login']);  
     $passwd = mysql_real_escape_string(( get_magic_quotes_gpc() )? stripslashes($_POST['password']): $_POST['password']);
    $sql = $this->dbInstance->select('users',array('salt'),array("login" => $login));
    print_r($sql);
    print md5(md5($passwd).$sql[0]['salt']);
    $sqlnext = $this->dbInstance->select('users',array('login'),array("login" => $login,"passw" => md5(md5($passwd).$sql[0]['salt'])));
    $_SESSION['login']=$sqlnext[0]['login'];
    print_r($sqlnext);
    $this->showAction();
    }
 
    function logoutAction()
    {
        $this->logoutUser();
        $this->redirect('/test/auth');
    }
    function regAction()
    {
     
     if (isset($_POST['nameUser']) && isset($_POST['passUser']) && isset($_POST['mail']))
{
     $nameUser = mysql_real_escape_string(( get_magic_quotes_gpc() )? stripslashes($_POST['nameUser']): $_POST['nameUser']);  
     $passUser = mysql_real_escape_string(( get_magic_quotes_gpc() )? stripslashes($_POST['passUser']): $_POST['passUser']);  
     $mail = mysql_real_escape_string(( get_magic_quotes_gpc() )? stripslashes($_POST['mail']): $_POST['mail']);  
     $query = mysql_query("SELECT COUNT(UID) FROM users WHERE login='".$mail."'");
     if(mysql_result($query, 0) > 0){
	 $err[] = "Пользователь с таким логином уже существует в базе данных";
	 }
     if(!filter_var($mail, FILTER_VALIDATE_EMAIL))
       $err[]='Неправильно введен почтовый ящик';
     if(strlen($passUser) < 3 or strlen($passUser) > 30)
	 $err[] = "Пароль должен быть не меньше 3-х символов и не больше 30";

     if(!isset($err)){
	$log['login']=$mail;   
	$log['salt']=$this->generate_password(3);
        $log['passw']=md5(md5($passUser).$log['salt']);
	$log['name']=$nameUser;
	$this->dbInstance->insert('users', $log);
	$_SESSION['login']=$mail;
	$this->showAction();
	exit();
     }
}    
      echo "<b>При регистрации произошли следующие ошибки:</b><br>";

        foreach($err AS $error)

        {

            echo $error."<br>";

        }
    }
     function generate_password($number)  
  {  
    $arr = array('a','b','c','d','e','f',  
                 'g','h','i','j','k','l',  
                 'm','n','o','p','r','s',  
                 't','u','v','x','y','z',  
                 'A','B','C','D','E','F',  
                 'G','H','I','J','K','L',  
                 'M','N','O','P','R','S',  
                 'T','U','V','X','Y','Z',  
                 '1','2','3','4','5','6',  
                 '7','8','9','0','.',',',  
                 '(',')','[',']','!','?',  
                 '&','^','%','@','*','$',  
                 '<','>','/','|','+','-',  
                 '{','}','`','~');  
    // ���������� ������  
    $pass = "";  
    for($i = 0; $i < $number; $i++)  
    {  
      // ��������� ��������� ������ �������  
      $index = rand(0, count($arr) - 1);  
      $pass .= $arr[$index];  
    }  
    return $pass;  
  }  
}
 
?>